home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 9.8 KB | 398 lines | [TEXT/MPS ] |
- /*
- File: Utilities.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
-
- #ifndef __BLJSTANDARDINCLUDES__
- #include "BLJStandardIncludes.h"
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
- #pragma segment Utilities
-
- /***********************************|****************************************/
-
- ostream& DumpHex (ostream& s, const void *pv, unsigned long size)
- {
- register char* p = (char*) pv;
-
- if (size > 1024)
- size = 1024;
-
- for (short i = 0; i < size; i += 16)
- {
- s << hexo << (long) (p + i) << ":" << deco;
- for (short j = 0; j < 16; ++j)
- {
- char c = * (char *) (p + i + j);
- if (i + j < size)
- s << hexo << ((c & 0xf0) >> 4) << (c & 0x0f) << " ";
- else
- s << " ";
- }
- s << " | ";
- for (j = 0; j < 16; ++j)
- {
- char c = ( * (char *) (p + i + j)) & 0x7f;
- if (c < 32) c = '•';
- if (i + j < size)
- s << c;
- }
- s << deco << endl;
- }
-
- return s;
- }
-
- //--------------------------------------------------------------------------------
-
- void GetRezMessage(short rezId, short problemIndex, RString& message)
- {
- Str255 theString;
- GetIndString(theString, rezId, problemIndex);
- message.charSet = smRoman;
- message.dataLength = (unsigned short) longmin(theString[0], kRStringMaxChars);
- BlockMove( &theString[1], (Ptr) &message.body, message.dataLength );
- }
-
- //--------------------------------------------------------------------------------
-
- unsigned long NowDateTime()
- {
- unsigned long now;
- GetDateTime(&now);
- return now;
- }
-
- /*-----------------------------------------------------------
- - MatchResString: Determines if a string exists in a
- - STR# list and returns the index
- -
- - theStr: string to search for
- - startIndex: inclusive start of the search
- - endIndex: inclusive end of search
- - resID: STR# res id to look in
- -
- - returns: the index of the string
- ------------------------------------------------------------
- */
- short MatchResString(const StringPtr theStr, short startIndex, short endIndex, short resID)
- {
- Str255 resStr;
-
- for (short i = startIndex; i <= endIndex; i++)
- {
- GetIndString(resStr, resID, i);
-
- if (EqualString(resStr, theStr, false, false))
- return(i);
- }
-
- return(-1);
- }
-
- //--------------------------------------------------------------------------------
- //
- // GetDirIDForFolder()
- // ===================
- //
- // Return the dirID for a folder, given the vRefNum of the disk and the folder name.
- // If the folder does not exist, return 0 for the dirID.
- //
- long GetDirIDForFolder(short vRefNum, long dirID, const StringPtr folderName)
- {
- CInfoPBRec paramBlock;
- long result = 0;
-
- paramBlock.dirInfo.ioVRefNum = vRefNum;
- paramBlock.dirInfo.ioDrDirID = dirID;
- paramBlock.dirInfo.ioFDirIndex = 0;
- paramBlock.dirInfo.ioNamePtr = (StringPtr) folderName;
-
- OSErr err = PBGetCatInfo(¶mBlock, false);
-
- if (err == noErr)
- if (BitTst(¶mBlock.dirInfo.ioFlAttrib, 3))
- result = paramBlock.dirInfo.ioDrDirID;
-
- return(result);
- }
-
-
- //--------------------------------------------------------------------------------
- //
- // GetIndexHFSInfo
- // ===============
- //
- // This function returns information about the index-th file in the directory
- // given by (vRefNum, dirRefNum). It returns the name of the file/directory in
- // theName, which must be a pointer to a Str32. If the entry is a file, then
- // refNum is a reference number for this file if the file is currently open, or
- // 0 if the file is not open. If this entry is a directory, then refNum is the
- // dirID for the directory. isFile is either kFile or kFolder or kNotFileOrFolder,
- // depending on what type of entry the given file is.
- //
- OSErr GetIndexHFSInfo(short index, short vRefNum, long dirRefNum, const StringPtr theName)
- {
- long refNum;
- short isFile;
- return GetIndexHFSInfo(index, vRefNum, dirRefNum, refNum, theName, isFile);
- }
-
- /***********************************|****************************************/
-
- OSErr GetIndexHFSInfo(short index, short vRefNum, long dirRefNum, long& refNum, const StringPtr theName, short& isFile)
- {
- CInfoPBRec paramBlock;
- OSErr err;
-
- // If we're getting info on an indexed file, then zero out the name.
- if ((index > 0) && (theName))
- {
- theName[0] = 0;
- }
-
- memset ( ¶mBlock, 0, sizeof(paramBlock));
- paramBlock.dirInfo.ioVRefNum = vRefNum;
- paramBlock.dirInfo.ioDrDirID = dirRefNum;
- paramBlock.dirInfo.ioFDirIndex = index;
- paramBlock.dirInfo.ioNamePtr = (StringPtr) theName;
-
- err = PBGetCatInfo(¶mBlock, false);
-
- if (err == noErr)
- {
- if (BitAnd(paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible) == 0)
- {
- SignedByte flags = paramBlock.dirInfo.ioFlAttrib;
- if (BitTst(&flags, 3))
- { // This is a folder
- refNum = paramBlock.dirInfo.ioDrDirID;
- isFile = kFolder;
- }
- else
- { // This a file
- refNum = paramBlock.dirInfo.ioFRefNum;
- isFile = kFile;
- }
- }
- else
- isFile = kNotFileOrFolder;
-
- return(noErr);
- }
-
- return(err);
- }
-
- //--------------------------------------------------------------------------------
- //
- // CreateFolderIfItDoesntExist()
- // =============================
- //
- // This function creates a folder in the given vRefNum, dirID with a given name
- // if it does not exist. The dirID of the folder (existing or newly-created) is
- // returned as the result of this function.
- //
- long CreateFolderIfItDoesntExist(short vRefNum, long parentDir, const StringPtr folderName)
- {
- long refNum;
- short isFile;
- Str255 temp;
-
- PLstrcpy( temp,folderName);
- OSErr err = GetIndexHFSInfo(0, vRefNum, parentDir, refNum, temp, isFile);
-
- if (err != noErr)
- {
- FSSpec spec;
- err = FSMakeFSSpec(vRefNum,parentDir, temp,&spec);
- err = FSpDirCreate(&spec,smRoman,&refNum);
- }
-
- return refNum;
- }
-
- //--------------------------------------------------------------------------------
- //
- // DeleteFilesInFolder()
- // =====================
- //
- // This function deletes all of the files in the given folder. It does not delete
- // any folders in the directory, nor does it recursively delete files in any
- // subfolders.
- //
- void DeleteFilesInFolder(short vRefNum, long folderID)
- {
- long refNum;
- short isFile;
- OSErr err = noErr;
- Str255 temp;
-
- do
- {
- FSSpec spec;
-
- err = GetIndexHFSInfo(1,vRefNum, folderID, refNum, temp, isFile);
-
- if ((err == noErr) && (isFile == kFile))
- {
- err = FSMakeFSSpec(vRefNum,folderID, temp,&spec);
- FSpDelete(&spec);
- }
- }
- while (err == noErr);
- }
-
-
- /***********************************|****************************************/
-
- void DeleteFilesInFolderOlderThan ( short vRefNum, long dirID, unsigned long deleteFilesBeforeDateTime)
- {
- OSErr err = noErr;
- unsigned short index = 1;
-
- do {
- CInfoPBRec paramBlock;
- Str255 fileName;
-
- fileName[0] = 0;
-
- memset ( ¶mBlock, 0, sizeof(paramBlock));
- paramBlock.dirInfo.ioVRefNum = vRefNum;
- paramBlock.dirInfo.ioDrDirID = dirID;
- paramBlock.dirInfo.ioFDirIndex = index;
- paramBlock.dirInfo.ioNamePtr = (StringPtr) &fileName;
-
- err = PBGetCatInfoSync ( ¶mBlock );
-
- if (err == noErr)
- {
- if (BitAnd(paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible) == 0 )
- {
- SignedByte flags = paramBlock.dirInfo.ioFlAttrib;
- if ( !(flags & 0x0010) )
- { // This a file
- if ( paramBlock.hFileInfo.ioFlMdDat < deleteFilesBeforeDateTime )
- if (HDelete ( vRefNum, dirID, fileName ) == noErr )
- {
- ForceFinderToUpdateFolder ( vRefNum, dirID );
- index -- ;
- }
- }
- }
- index ++;
- }
- } while ( err == noErr );
- }
-
- //--------------------------------------------------------------------------------
- //
- // ForceFinderToUpdateFolder()
- // ===========================
- //
- // Given a vRefNum and dirID, force the Finder to flush any cached info
- // for the folder by touching the modification date for this folder.
- //
- OSErr ForceFinderToUpdateFolder(short vRefNum, long dirID)
- {
- OSErr err;
- CInfoPBRec pb;
-
- pb.dirInfo.ioVRefNum = vRefNum;
- pb.dirInfo.ioDrDirID = dirID;
- pb.dirInfo.ioNamePtr = nil;
- pb.dirInfo.ioFDirIndex = -1;
- err = PBGetCatInfoSync((CInfoPBPtr) &pb);
-
- if (err == noErr)
- {
- GetDateTime(&pb.dirInfo.ioDrMdDat);
- err = PBSetCatInfoSync((CInfoPBPtr) &pb);
- }
-
- return err;
- }
-
- /***********************************|****************************************/
- //
- // Move a file named fileName on the volume vRefNum from the directory sourceDirID to
- // the directory destinationDirID.
- //
- OSErr MoveHFSFile(short vRefNum, long sourceDirID, const StringPtr fileName, long destinationDirID)
- {
- OSErr err = HDelete ( vRefNum, destinationDirID, fileName );
- #if debug
- if ( err != fnfErr )
- chris << "MoveHFSFile is deleting file “" << fileName << "”; is that alright??\n";
- #endif
-
- // now move the file
- CMovePBRec pb;
-
- memset(&pb, 0, sizeof(pb));
- pb.ioNamePtr = fileName;
- pb.ioVRefNum = vRefNum;
- pb.ioDirID = sourceDirID;
-
- pb.ioNewName = nil; // use ioNewDirID
- pb.ioNewDirID = destinationDirID;
-
- err = PBCatMoveSync(&pb);
-
- if (err == noErr) {
- ForceFinderToUpdateFolder(vRefNum, sourceDirID);
- ForceFinderToUpdateFolder(vRefNum, destinationDirID);
- };
-
- return err;
- }
-
- /***********************************|****************************************/
-
- pascal void BLJDUc2rString (const char* cStr, CharacterSet charSet, RString *rStr, unsigned short rStrLength) {
- #if false
- DUc2rString(cStr,charSet,rStr,rStrLength);
- #endif
- rStr->dataLength = strlen(cStr);
- if (rStrLength < rStr->dataLength)
- rStr->dataLength = rStrLength;
- strncpy((char*) &(rStr->body[0]),cStr,rStr->dataLength);
- rStr->charSet = charSet;
- }
-
- /***********************************|****************************************/
-
- #ifndef THINK_CPLUS
- #ifndef __CURSORCTL__
- #include "CursorCtl.h"
- #endif
-
-
- void Busy ( short i )
- {
- SpinCursor ( i );
- }
- #endif
- /***********************************|****************************************/
-